<template>
  <div class="Team page" ref="webview">
    <van-nav-bar title="市场团队" class="bg_white" left-arrow @click-left="$router.push({'path': '/my'})" />
    <div class="content">
      <div class="card-flex">
        <template v-for="(item,index) in level">
          <span class="c" :style="'background-color:'+item.bgcolor" :key="index">{{item.name}}</span>
        </template>
      </div>
      <div class="team_top" @click="goTop">直达顶层</div>
      <div class="card-rows card-rows-1" v-if="cardRows_1.level_name">
        <div class="card j-card" :style="'border: 1px solid '+cardRows_1.color">
          <div class="title" :style="'background-color:'+cardRows_1.color">{{cardRows_1.level_name}}</div>
          <div class="text" :style="'border-bottom: 1px solid '+cardRows_1.color">总业绩:{{cardRows_1.total_money}}</div>
          <div class="qu">
            <div class="name">
              <span>A区</span>
              <span>{{cardRows_1.left_money}}</span>
            </div>
            <div class="name" :style="'border-left: 1px solid '+cardRows_1.color">
              <span>B区</span>
              <span>{{cardRows_1.right_money}}</span>
            </div>
          </div>
        </div>
      </div>
      <div class="line" v-if="cardRows_1.level_name">
        <div class="line-1"></div>
        <div class="line-2"></div>
      </div>
      <div class="card-rows card-rows-2">
        <template v-for="(item,index) in cardRows_2">
          <div class="card" :key="index" :style="'border: 1px solid '+item.color" @click="getMarketTeam(item)">
            <div class="title" :style="'background-color:'+item.color">{{item.level_name}}</div>
            <div class="text" v-if="item.total_money>=0" :style="'border-bottom: 1px solid '+item.color">{{item.member_no}}</div>
            <div class="text" v-if="item.total_money>=0" :style="'border-bottom: 1px solid '+item.color">总业绩:{{item.total_money}}</div>
            <div class="qu" v-if="item.left_money>=0 || item.right_money>=0">
              <div class="name">
                <span>A区</span>
                <span>{{item.left_money}}</span>
              </div>
              <div class="name" :style="'border-left: 1px solid '+item.color">
                <span>B区</span>
                <span>{{item.right_money}}</span>
              </div>
            </div>
          </div>
        </template>
      </div>
      <div class="line" v-if="cardRows_3.length> 0">
        <div class="to">
          <div class="line-1"></div>
          <div class="line-3"></div>
        </div>
        <div class="to" v-if="cardRows_3.length >2">
          <div class="line-1"></div>
          <div class="line-3"></div>
        </div>
      </div>
      <div class="card-rows card-rows-3" :class="{'rows-active':cardRows_3.length <=2}">
        <template v-for="(item,index) in cardRows_3">
          <div class="card" :key="index" :style="'border: 1px solid '+item.color">
            <div class="title" :style="'background-color:'+item.color" :class="{active: !item.value}">{{item.level_name}}</div>
            <div class="text" v-if="item.total_money>=0" :style="'border-bottom: 1px solid '+item.color">{{item.member_no}}</div>
            <div class="text" v-if="item.total_money>=0" :style="'border-bottom: 1px solid '+item.color">总业绩:{{item.total_money}}</div>
            <div class="qu" v-if="item.left_money>=0 || item.right_money>=0">
              <div class="name">
                <span>A区</span>
                <span>{{item.left_money}}</span>
              </div>
              <div class="name" :style="'border-left: 1px solid '+item.color">
                <span>B区</span>
                <span>{{item.right_money}}</span>
              </div>
            </div>
          </div>
        </template>
      </div>
    </div>
    <div class="submiBtn">
      <van-button class="btn" color="#125DFF" round :loading="isLoading " @click.native="$router.push({'path': '/my/register'})">注册新会员</van-button>
    </div>
  </div>
</template>
<script>
import { mapGetters } from 'vuex';
export default {
  name: 'Team',
  data () {
    return {
      isLoading: false,
      cardRows_1: {},
      cardRows_2: [],
      cardRows_3: [],
      topId: null
    };
  },
  computed: {
    ...mapGetters(['userInfo', 'level'])
  },
  created () {
    this.getMarketTeam();
  },
  methods: {
    goTop () {
      this.getMarketTeam(this.topId);
    },
    getMarketTeam (dataItem) {
      if (dataItem && dataItem.level_name === '未开通') {
        return;
      }
      var id = dataItem && dataItem.id;
      var cardRows = [];
      this.cardRows_1 = {};
      this.cardRows_2 = [];
      this.cardRows_3 = [];
      this.$toast.loading({ duration: 0 });
      this.$API.user.marketTeam({ mid: id || this.userInfo.id }).then(res => {
        if (!res.code) {
          cardRows = [res.data.map];
          cardRows.map((itemRes1, index) => {
            this.cardRows_1 = itemRes1;
            itemRes1.sub && itemRes1.sub.map((itemRes2, key) => {
              this.cardRows_2.push(itemRes2);
              itemRes2.sub && itemRes2.sub.map((itemRes3, key) => {
                this.cardRows_3.push(itemRes3);
              });
            });
          });
          if (!id) {
            this.topId = cardRows[0].id;
          }
          this.$toast.clear();
        } else {
          setTimeout(() => {
            this.$toast(res.message);
          }, 400);
        }
      });
    }
  }
};
</script>
<style lang="less" scoped>
.Team {
  display: flex;
  flex-direction: column;
  .submiBtn {
    position: absolute;
    bottom: 100px;
    left: 50px;
    right: 50px;
    .btn {
      width: 100%;
    }
  }
  .content {
    flex: 1;
    background-color: #fff;
    position: relative;
    .team_top {
      position: absolute;
      top: 114px;
      right: 50px;
      background-color: #007aff;
      padding: 10px 14px;
      font-size: 24px;
      border-radius: 6px;
      color: #fff;
    }
    .line {
      position: relative;
      height: 55px;
      .to {
        height: 55px;
        float: left;
        width: 50%;
        position: relative;
      }
    }
    .line-1 {
      width: 50px; /*no*/
      height: 29px;
      border-left: 1px solid #666; /*no*/
      position: absolute;
      left: 50%;
      top: 0; /*no*/
      margin-left: -1px;
      z-index: 99;
    }
    .line-2,
    .line-3 {
      height: 30px;
      margin: -1px auto 0; /*no*/
      border: 1px solid #666; /*no*/
      border-bottom: none;
      position: absolute;
      left: 50%;
      bottom: -1px; /*no*/
      z-index: 96;
    }
    .line-2 {
      width: 380px;
      margin-left: -190px;
    }
    .line-3 {
      width: 200px;
      margin-left: -100px;
    }
    .card-flex {
      display: flex;
      padding: 30px 20px;
      .c {
        flex: 1;
        display: block;
        text-align: center;
        margin: 0 35px;
        color: #fff;
        border-radius: 6px;
        padding: 10px 0;
        font-size: 24px;
      }
      .p-c {
        background-color: #00aa7f;
      }
      .y-c {
        background-color: #0088fe;
      }
      .j-c {
        background-color: #ffc14e;
      }
      .z-c {
        background-color: #ff418a;
      }
    }
    .card-rows-1 {
      .card {
        margin: 0 50px;
        width: 244px;
      }
    }
    .card-rows-2 {
      .card {
        width: 50%;
        margin: 0 66px;
      }
    }
    .card-rows-3 {
      .card {
        margin: 0 10px;
        width: 200px;
      }
    }
    .card-rows {
      display: flex;
      justify-content: center;
      .card {
        border-radius: 6px;
        .title {
          padding: 10px 0;
          text-align: center;
          color: #fff;
        }
        .title {
          background-color: #b7b5b5;
        }
        .text {
          text-align: center;
          color: #000;
          padding: 10px 0;
          font-size: 20px;
        }
        .qu {
          display: flex;
          .name {
            flex: 1;
            text-align: center;
            padding: 15px 0;
            position: relative;
            span {
              display: block;
              color: #000;
            }
            // &::after {
            //   display: block;
            //   content: '';
            //   width: 1px; /*no*/
            //   height: 25px;
            //   border-left: 1px solid #666; /*no*/
            //   position: absolute;
            //   bottom: -26px;
            //   left: 50%;
            //   z-index: 999;
            // }
          }
        }
      }
    }
    .card-rows.rows-active {
      width: 50%;
    }
  }
}
</style>
